//
//  ViewController.m
//  绘制带圆环的图片
//
//  Created by 伍银荣 on 2021/7/3.
//  Copyright © 2021 com.BitmapContextWithCirle. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"----touchesBegan----");
    UIImage *image = [UIImage imageNamed:@"樱桃小丸子"];
    //设置间隙
    CGFloat margin = 10;
    //获取图形上下文的大小
    CGSize contextSize = CGSizeMake(image.size.width + margin * 2, image.size.height + margin * 2);
    //开启图片类型的图形上下文
    UIGraphicsBeginImageContextWithOptions(contextSize, NO, 0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //拼接路径,同时添加到图形上下文中(此时绘制一个线宽为10的圆环)
    CGContextAddArc(ctx, contextSize.width * 0.5, contextSize.height * 0.5, (image.size.width + margin) * 0.5, 0, M_PI * 2, 1);
    //设置线宽为10
    CGContextSetLineWidth(ctx, 10);
    //渲染
    CGContextStrokePath(ctx);
    
    //开始绘制图片
    CGContextAddArc(ctx, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5, 0, M_PI * 2, 1);
    
    //把图片画上去
    [image drawAtPoint:CGPointMake(margin, margin)];
    
    //获取图片
    image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片类型的图形上下文
    UIGraphicsEndImageContext();
    
    //把图片保存到相册中
    UIImageWriteToSavedPhotosAlbum(image, NULL, NULL, NULL);
}

@end
